package com.itheima.reggie.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.User;
import com.itheima.reggie.service.UserService;
import com.itheima.reggie.utils.SMSUtils;
import com.itheima.reggie.utils.ValidateCodeUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 发送手机验证码
     * @param user
     * @return
     */
    @PostMapping("/sendMsg")
    public R<String> sendMsg(@RequestBody User user){
        //获取手机号
        String phone = user.getPhone();
        if(StringUtils.isNotEmpty(phone)){
            //生成随机验证码
            String code = ValidateCodeUtils.generateValidateCode(4).toString();
            log.info("验证码："+code);
            //发送短信

            redisTemplate.opsForValue().set("code:"+phone,code,5, TimeUnit.MINUTES);

            //验证码保存到session
      //      session.setAttribute(phone,code);

            return R.success("短信发送成功");
        }
        return R.error("短信发送失败");
    }

    /**
     * 移动端登录
     * @param map
     * @return
     */
    @PostMapping("/login")
    public R<User> login(@RequestBody Map<String ,String> map,HttpSession session){
        log.info("登录:{}",map);
        String phone = map.get("phone");
        String code =  map.get("code");
        if(StringUtils.isEmpty(phone)||StringUtils.isEmpty(code))
            return R.error("手机号或验证码不得为空");


        //正确的验证码
        String realCode = (String) redisTemplate.opsForValue().get("code:"+phone);
        if(realCode!=null&&realCode.equals(code)){
            //是否新用户
            LambdaQueryWrapper<User> queryWrapper=new LambdaQueryWrapper<>();
            queryWrapper.eq(User::getPhone,phone);
            User one = userService.getOne(queryWrapper);
            if(one==null){
                //用户不存在
                User user = new User();
                user.setPhone(phone);
                userService.save(user);
            }else{
                session.setAttribute("user",one.getId());
                redisTemplate.delete("code:"+phone);
                return R.success(one);
            }
        }
        return R.error("验证码输入错误");
    }

    /**
     * 移动端推出登录
     * @param httpSession
     * @return
     */
    @PostMapping("/loginout")
    public R<String> loginout(HttpSession httpSession){
        httpSession.removeAttribute("user");
        return R.success("已退出登录");


    }
}
